# from PIL import Image, ImageDraw
# import math
# import numpy as np
# import imageio
#
# def calcSphereXY2XYZ(px, py, maxHeight, longOffset):
#     v0x= np.array(px)
#     v0y= np.array(py)
#     v03= np.subtract(v0x, maxHeight)
#     v04= np.subtract(v0y, maxHeight)
#     v1x= np.true_divide(v03, maxHeight)
#     v1y= np.true_divide(v04, maxHeight)
#     # print(max(v1x), min(v1x))
#     v07= np.power(v1x,2)
#     v08= np.power(v1y,2)
#     v09= np.add(v07,v08)
#     v0a= np.subtract(1,v09)
#     v1z= np.power(v0a,1/2)                                  # z
#     # print('z:', max(v1z), min(v1z))
#     v1lat= np.multiply(v1y, math.pi/2)                      # lat
#     v0lon= np.arctan2(v1z, -v1x)
#     v1lon= np.add(v0lon, longOffset)                       # long
#     v2lon= np.fmod(v1lon, math.pi*2)                       # long
#     return v2lon, v1lat
#
# def calcShpereLatLong2XY(vlon, vlat, width, height):
#     v3x0=np.multiply(vlon, width/2/math.pi)
#     v3y0=np.multiply(vlat, height/math.pi)
#     v3y1=np.add(v3y0, height/2)
#     v3x2=v3x0.astype(np.integer)
#     v3y2=v3y1.astype(np.integer)
#     return v3x2, v3y2
#
#
#
# def getPic(a):
#     # imgBack= Image.open('地球3.jpg')
#     imgBack= Image.open('EARTH_small.bmp')
#     width= imgBack.size[0]
#     height= imgBack.size[1]
#     imgBack= imgBack.convert('RGBA')
#     arrayBack= np.array(imgBack)
#
#     img= Image.new('RGBA', (300,300), 'black')
#     w= img.size[0]
#     h= img.size[1]
#     pxList=[]
#     pyList=[]
#     for i in range(w):
#         for j in range(h):
#             r= math.sqrt((i-w/2)**2+(j-h/2)**2)
#             if r<150:
#                 pxList.append(i)
#                 pyList.append(j)
#
#     nplon, nplat= calcSphereXY2XYZ(pxList, pyList, h/2, a)
#     npx, npy= calcShpereLatLong2XY(nplon, nplat, width-1, height)
#     color= arrayBack[npy, npx]
#     for i in range(len(pxList)):
#         x= pxList[i]
#         y= pyList[i]
#         cc=color[i]
#         # print(cc)
#         cc= tuple(cc)
#         img.putpixel((x,y), cc)
#     return img
#
# if __name__=='__main__':
#     frames=[]
#     for i in range(0, 360, 10):
#         a= -i*math.pi/ 180
#         img= getPic(a)
#         str1= 'temp%03d.png'%i
#         img.save(str1)
#         im = imageio.imread(str1)
#         frames.append(im)
#         # img.show()
#     imageio.mimsave('earth.gif', frames, 'GIF', duration=0.25)


# -*- coding: utf-8 -*-

import wx
import numpy as np
from PIL import Image

from wxgl.scene import *
from wxgl.colormap import *


class mainFrame(wx.Frame):
    '''程序主窗口类，继承自wx.Frame'''

    def __init__(self):
        wx.Frame.__init__(self, None, -1, '地球模型上的2019-nCoV疫情地图', style=wx.DEFAULT_FRAME_STYLE)
        self.Maximize()

        # 从等经纬地图上读取经纬度网格上的每一个格点的颜色
        pic=Image.open('e0.jpg')
        c = np.array(pic) / 255

        # 生成和等经纬地图分辨率一致的经纬度网格，计算经纬度网格上的每一个格点的空间坐标(x,y,z)
        lats, lons = np.mgrid[np.pi / 2:-np.pi / 2:complex(0, c.shape[0]), 0:2 * np.pi:complex(0, c.shape[1])]
        x = np.cos(lats) * np.cos(lons)
        y = np.cos(lats) * np.sin(lons)
        z = np.sin(lats)

        self.scene = WxGLScene(self, r"C:\Windows\Fonts\simfang.ttf", bg=[0, 0, 0, 0])
        self.scene.setPosture(elevation=0, azimuth=120, save=True)
        self.master = self.scene.addRegion((0, 0, 1, 1))
        self.master.drawMesh('earth', x, y, z, c)
        self.master.update()


class mainApp(wx.App):
    def OnInit(self):
        self.Frame = mainFrame()
        self.Frame.Show()
        return True


if __name__ == "__main__":
    app = mainApp()
    app.MainLoop()

